package com.tjetc.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.tjetc.common.JsonResult;
import com.tjetc.dao.DailyAmountMapper;
import com.tjetc.dao.MonthlyAmountMapper;
import com.tjetc.entity.DailyAmount;
import com.tjetc.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.Local;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("admin/money")
public class MoneyController {
    // 这里因为只需要进行单表查询，所以没有加service
    @Autowired
    private MonthlyAmountMapper monthlyAmountMapper;

    @Autowired
    private DailyAmountMapper dailyAmountMapper;

    @Autowired
    private OrderService orderService;

    // 获取一整月的收入情况
    @RequestMapping("getOrderInMonth")
    public JsonResult getOrderInMonth(@RequestParam(value = "merchantId", required = false) Integer merchantId,
                                      @RequestParam(value = "time", required = false)
                                          @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
                                          @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime time) {
        List<DailyAmount> dailyAmounts = dailyAmountMapper.selectByMonthTime(time, merchantId);
        return new JsonResult(0, "", dailyAmounts);
    }

    // 获取一整天所有的订单详情
    @RequestMapping("getOrderInDay")
    public JsonResult getOrderInDay(@RequestParam(value = "merchantId", required = false) Integer merchantId,
                                    @RequestParam(value = "time", required = false)
                                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime time) {
        return new JsonResult(0, "", orderService.selectByOrderAndDay(merchantId, time));
    }

    // 刷新查询，直接调用定时任务，方便调试
    @RequestMapping("refresh")
    public JsonResult doRefresh(@RequestParam("merchantId") Integer id,
                                @RequestParam(value = "time", required = false)
                                @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime time) {
        orderService.refresh(time, id);
        return new JsonResult(0, "", true);
    }

    // 刷新查询，直接调用定时任务，方便调试
    @RequestMapping("refreshDay")
    public JsonResult doRefreshDay(@RequestParam("merchantId") Integer id,
                                   @RequestParam(value = "time", required = false)
                                   @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime time) {
        orderService.refreshDay(time, id);
        return new JsonResult(0, "", true);
    }

    // 获取管理端的财务基本信息
    @RequestMapping("getMoneyInfo")
    public JsonResult getMoneyInfo(@RequestParam(value = "time")
                                       @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime time){
        return orderService.getMoneyInfo(time);
    }
}
